package cn.edu.scnu.impl;

import cn.edu.scnu.entity.*;
import cn.edu.scnu.mapper.MovieMapper;
import cn.edu.scnu.mapper.MoviePlayerMapper;
import cn.edu.scnu.mapper.MovieStatsMapper;
import cn.edu.scnu.service.MovieService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private  MoviePlayerMapper moviePlayerMapper;
    @Autowired
    private MovieStatsMapper movieStatsMapper;

    @Override
    public List<Movie> findMoviesByActorName(String actorName) {
        return movieMapper.findMoviesByActorName(actorName);
    }

    /**
     * 根据类型、地区获取电影列表
     * @param page
     * @param size
     * @param type
     * @param region
     * @return
     */
    @Override
    public List<Movie> findMovies(int page, int size, String type, String region) {
        int offset = (page - 1) * size;
        return movieMapper.findMovies(size, offset, type, region);
    }

    /**
     * 获取电影总数
     * @param type
     * @param region
     * @return
     */
    @Override
    public int getMovieCount(String type, String region) {
        return movieMapper.countMovies(type, region);
    }

    @Override
    public boolean canWatchMovie(Member member, String movieId) {
        Movie movie = movieMapper.selectById(movieId);
        if (movie == null) {
            return false;
        }
        if(movie.getVipOnly()==0){
            return true;
        }
        if (movie.getVipOnly() == 1 && member.getVip()!=1) {
            return false;
        }
        return true;
    }
    @Override
    public MovieDetails getMovieDetails(String movieId) {
        Movie movie = movieMapper.selectById(movieId);
        MovieStats movieStats = movieStatsMapper.selectByMovieId(movieId);
        MoviePlayer moviePlayer = moviePlayerMapper.selectByMovieId(movieId);
        MovieDetails movieDetails = new MovieDetails();
        movieDetails.setMovie(movie);
        movieDetails.setMovieStats(movieStats);
        movieDetails.setMoviePlayer(moviePlayer);

        return movieDetails;
    }
}
